package com.app.controller;

import com.app.common.Result;
import com.app.model.AreaModel;
import com.app.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

@Controller
@RestController
@RequestMapping("/sbbz/area")
public class AreaController {
    @Autowired
    private AreaService areaService;


    /**
     * 选择城镇
     *
     * @return {@link Result}<{@link Object}>
     */
    @GetMapping("/selectTown")
    public Result<Object> selectTown() {
        return Result.success(areaService.selectTown());
    }


    /**
     * 选择村庄
     *
     * @param id id
     * @return {@link Result}<{@link Object}>
     */
    @GetMapping("/selectVillage")
    public Result<Object> selectVillage(Integer id) {
        return Result.success(areaService.selectVillage(id));
    }


    /**
     * 选择树
     *
     * @return {@link Result}<{@link Object}>
     */
    @GetMapping("/selectTree")
    public Result<Object> selectTree() {
        List<AreaModel> areaModels = areaService.selectTree();//全查
        if (areaModels == null) {
            return Result.error(500, "暂无数据");
        }
        List<Integer> arr1 = new ArrayList<>();
        for (int i = 0; i < areaModels.size(); i++) {
            arr1.add(areaModels.get(i).getId());
        }
        Integer ArrId = 1;
        boolean result1 = arr1.contains(ArrId);
        List<Integer> pidArr1 = new ArrayList<>();
        if (!result1) {
            for (int i = 0; i < areaModels.size(); i++) {

                boolean PidResult = arr1.contains(areaModels.get(i).getPid());
                if (!PidResult) {
                    pidArr1.add(areaModels.get(i).getPid());
                }
            }
            pidArr1.add(ArrId);
            arr1.addAll(pidArr1);
            LinkedHashSet<Integer> hashSet = new LinkedHashSet<>(arr1);
            ArrayList<Integer> listWithoutDuplicates = new ArrayList<>(hashSet);
            List<AreaModel> permissionModels1 = new ArrayList<>();
            for (int i = 0; i < listWithoutDuplicates.size(); i++) {
                AreaModel areaModel = areaService.selectById(listWithoutDuplicates.get(i));
                permissionModels1.add(areaModel);
            }
            return Result.success(findChildren(permissionModels1, 0));
        }
        return Result.success(findChildren(areaModels, 0));
    }

    private List<AreaModel> findChildren(List<AreaModel> modelList, Integer pid) {
        List<AreaModel> personModelList = new ArrayList<>();
        for (int i = 0; i < modelList.size(); i++) {
            if (modelList.get(i).getPid().equals(pid)) {
                AreaModel model = modelList.get(i);
                List<AreaModel> modelList1 = findChildren(modelList, model.getId());
                for (int j = 0; j < modelList1.size(); j++) {
                    model.setChildren(modelList1);
                }
                personModelList.add(model);
            }
        }
        return personModelList;
    }

}
